package com.ketaichan.zolon_printer.models;

import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Order {
    int id;
    int shopId;
    String tableName;
    String areaName;
    int tableId;
    int eatNumber;
    String seqNumber;
    int eatWay;
    boolean prePay;
    int otherPackagePrice;
    int otherPrice;
    int otherPriceDiscountPrice;
    String no;
    long createTime;
    String userRemark;
    int status;
    int productCount;
    long finishTime;
    long cancelTime;
    boolean commentStatus;
    boolean payStatus;
    int payOrderId;
    long payTime;
    long payExpireTime;
    String payChannelCode;
    String payChannelName;
    int totalPrice;
    int sumPrice;
    int discountPrice;
    int actualDiscount;
    int deliveryPrice;
    int adjustPrice;
    int payPrice;
    int deliveryType;
    String logisticsNo;
    long deliveryTime;
    long receiveTime;
    String settlementName;
    long settlementTime;
    String receiverName;
    String receiverMobile;
    int receiverAreaId;
    String receiverAreaName;
    String receiverDetailAddress;
    int pickUpStoreId;
    int couponId;
    int couponPrice;
    int pointPrice;
    List<SubOrderItem> itemBatchs;
    String eatWayName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getShopId() {
        return shopId;
    }

    public void setShopId(int shopId) {
        this.shopId = shopId;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public int getTableId() {
        return tableId;
    }

    public void setTableId(int tableId) {
        this.tableId = tableId;
    }

    public int getEatNumber() {
        return eatNumber;
    }

    public void setEatNumber(int eatNumber) {
        this.eatNumber = eatNumber;
    }

    public String getSeqNumber() {
        return seqNumber;
    }

    public void setSeqNumber(String seqNumber) {
        this.seqNumber = seqNumber;
    }

    public int getEatWay() {
        return eatWay;
    }

    public void setEatWay(int eatWay) {
        this.eatWay = eatWay;
    }

    public boolean isPrePay() {
        return prePay;
    }

    public void setPrePay(boolean prePay) {
        this.prePay = prePay;
    }

    public int getOtherPackagePrice() {
        return otherPackagePrice;
    }

    public void setOtherPackagePrice(int otherPackagePrice) {
        this.otherPackagePrice = otherPackagePrice;
    }

    public int getOtherPrice() {
        return otherPrice;
    }

    public void setOtherPrice(int otherPrice) {
        this.otherPrice = otherPrice;
    }

    public int getOtherPriceDiscountPrice() {
        return otherPriceDiscountPrice;
    }

    public void setOtherPriceDiscountPrice(int otherPriceDiscountPrice) {
        this.otherPriceDiscountPrice = otherPriceDiscountPrice;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public String getUserRemark() {
        return userRemark;
    }

    public void setUserRemark(String userRemark) {
        this.userRemark = userRemark;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getProductCount() {
        return productCount;
    }

    public void setProductCount(int productCount) {
        this.productCount = productCount;
    }

    public long getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(long finishTime) {
        this.finishTime = finishTime;
    }

    public long getCancelTime() {
        return cancelTime;
    }

    public void setCancelTime(long cancelTime) {
        this.cancelTime = cancelTime;
    }

    public boolean isCommentStatus() {
        return commentStatus;
    }

    public void setCommentStatus(boolean commentStatus) {
        this.commentStatus = commentStatus;
    }

    public boolean isPayStatus() {
        return payStatus;
    }

    public void setPayStatus(boolean payStatus) {
        this.payStatus = payStatus;
    }

    public int getPayOrderId() {
        return payOrderId;
    }

    public void setPayOrderId(int payOrderId) {
        this.payOrderId = payOrderId;
    }

    public long getPayTime() {
        return payTime;
    }

    public void setPayTime(long payTime) {
        this.payTime = payTime;
    }

    public long getPayExpireTime() {
        return payExpireTime;
    }

    public void setPayExpireTime(long payExpireTime) {
        this.payExpireTime = payExpireTime;
    }

    public String getPayChannelCode() {
        return payChannelCode;
    }

    public void setPayChannelCode(String payChannelCode) {
        this.payChannelCode = payChannelCode;
    }

    public String getPayChannelName() {
        return payChannelName;
    }

    public void setPayChannelName(String payChannelName) {
        this.payChannelName = payChannelName;
    }

    public int getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(int totalPrice) {
        this.totalPrice = totalPrice;
    }

    public int getSumPrice() {
        return sumPrice;
    }

    public void setSumPrice(int sumPrice) {
        this.sumPrice = sumPrice;
    }

    public int getDiscountPrice() {
        return discountPrice;
    }

    public void setDiscountPrice(int discountPrice) {
        this.discountPrice = discountPrice;
    }

    public int getActualDiscount() {
        return actualDiscount;
    }

    public void setActualDiscount(int actualDiscount) {
        this.actualDiscount = actualDiscount;
    }

    public int getDeliveryPrice() {
        return deliveryPrice;
    }

    public void setDeliveryPrice(int deliveryPrice) {
        this.deliveryPrice = deliveryPrice;
    }

    public int getAdjustPrice() {
        return adjustPrice;
    }

    public void setAdjustPrice(int adjustPrice) {
        this.adjustPrice = adjustPrice;
    }

    public int getPayPrice() {
        return payPrice;
    }

    public void setPayPrice(int payPrice) {
        this.payPrice = payPrice;
    }

    public int getDeliveryType() {
        return deliveryType;
    }

    public void setDeliveryType(int deliveryType) {
        this.deliveryType = deliveryType;
    }

    public String getLogisticsNo() {
        return logisticsNo;
    }

    public void setLogisticsNo(String logisticsNo) {
        this.logisticsNo = logisticsNo;
    }

    public long getDeliveryTime() {
        return deliveryTime;
    }

    public void setDeliveryTime(long deliveryTime) {
        this.deliveryTime = deliveryTime;
    }

    public long getReceiveTime() {
        return receiveTime;
    }

    public void setReceiveTime(long receiveTime) {
        this.receiveTime = receiveTime;
    }

    public String getSettlementName() {
        return settlementName;
    }

    public void setSettlementName(String settlementName) {
        this.settlementName = settlementName;
    }

    public long getSettlementTime() {
        return settlementTime;
    }

    public void setSettlementTime(long settlementTime) {
        this.settlementTime = settlementTime;
    }

    public String getReceiverName() {
        return receiverName;
    }

    public void setReceiverName(String receiverName) {
        this.receiverName = receiverName;
    }

    public String getReceiverMobile() {
        return receiverMobile;
    }

    public void setReceiverMobile(String receiverMobile) {
        this.receiverMobile = receiverMobile;
    }

    public int getReceiverAreaId() {
        return receiverAreaId;
    }

    public void setReceiverAreaId(int receiverAreaId) {
        this.receiverAreaId = receiverAreaId;
    }

    public String getReceiverAreaName() {
        return receiverAreaName;
    }

    public void setReceiverAreaName(String receiverAreaName) {
        this.receiverAreaName = receiverAreaName;
    }

    public String getReceiverDetailAddress() {
        return receiverDetailAddress;
    }

    public void setReceiverDetailAddress(String receiverDetailAddress) {
        this.receiverDetailAddress = receiverDetailAddress;
    }

    public int getPickUpStoreId() {
        return pickUpStoreId;
    }

    public void setPickUpStoreId(int pickUpStoreId) {
        this.pickUpStoreId = pickUpStoreId;
    }

    public int getCouponId() {
        return couponId;
    }

    public void setCouponId(int couponId) {
        this.couponId = couponId;
    }

    public int getCouponPrice() {
        return couponPrice;
    }

    public void setCouponPrice(int couponPrice) {
        this.couponPrice = couponPrice;
    }

    public int getPointPrice() {
        return pointPrice;
    }

    public void setPointPrice(int pointPrice) {
        this.pointPrice = pointPrice;
    }

    public List<SubOrderItem> getItemBatchs() {
        return itemBatchs;
    }

    public void setItemBatchs(List<SubOrderItem> itemBatchs) {
        this.itemBatchs = itemBatchs;
    }

    public String getEatWayName() {
        return eatWayName;
    }

    public void setEatWayName(String eatWayName) {
        this.eatWayName = eatWayName;
    }

    /**
     * 订单中所有商品，多个子订单归类汇总
     * @return List<OrderGoodsItem> 汇总后的所有商品
     */
    public List<OrderGoodsItem> getOrderGoodsList() {
        List<OrderGoodsItem> goodsList = new ArrayList<OrderGoodsItem>();
        if (this.itemBatchs != null && this.itemBatchs.size() > 0) {
            for (int i = 0; i < this.itemBatchs.size(); i++) {
                SubOrderItem subOrder = this.itemBatchs.get(i);
                if (subOrder.items != null && subOrder.items.size() > 0) {
                    goodsList.addAll(subOrder.items);
                }
            }
        }
        return goodsList;
    }

    public List<OrderGoodsItem> getOrderMergedGoodsList() {
        List<OrderGoodsItem> list = this.getOrderGoodsList();
        Map<Integer, OrderGoodsItem> productMap = new HashMap<>();

        for (OrderGoodsItem item: list) {
            int id = item.getSkuId();
            if (productMap.containsKey(id)) {
                OrderGoodsItem goods = productMap.get(id);
                float count1 = Float.parseFloat(goods.getCount());
                float count2 = Float.parseFloat(item.getCount());
                goods.setCount((count1 + count2) + "");
            } else {
                Gson gson = new Gson();
                productMap.put(id, gson.fromJson(gson.toJson(item), OrderGoodsItem.class));
            }
        }

        List<OrderGoodsItem> goodsList = new ArrayList<>(productMap.values());
        return goodsList;
    }

    public float getGoodsCount() {
        float count = 0;
        for (int i = 0; i < this.getOrderGoodsList().size(); i++) {
            count += Float.parseFloat(this.getOrderGoodsList().get(i).count);
        }
        return count;
    }

    public String getTableNameWithArea() {
        String tableName = "";
        if (this.getAreaName() != null) {
            tableName = this.getAreaName();
        }
        if (this.getTableName() != null) {
            tableName += this.getTableName();
        }
        return tableName.isEmpty() ? "无" : tableName;
    }

    public String getFormatCreateTime() {
        // 将时间戳转换为 LocalDateTime
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                    .withZone(ZoneId.systemDefault());
            // 转换时间戳为日期字符串
            return formatter.format(Instant.ofEpochMilli(createTime));
        } else {
            // 定义日期格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 转换时间戳为日期字符串
            return sdf.format(new Date(createTime));
        }
    }
}

